<?php

/**
 *
 * PHP API 商品咨询控制器
 * @author jeff
 * powered by jeff liu 2011-06-08
 */
class Goods_qaApp extends ApiApp
{
    public $_goods_qa_mod;

    function __construct()
    {
        $this->Goods_qaApp();
    }

    function Goods_qaApp()
    {
        parent::__construct();
        $this->_goods_qa_mod =& api_model('goodsqa');
    }

    /**
     * 获得商品咨询
     * @id string  required 多个id用","分割
     */
    function apiGetGoodsqa()
    {
        $goods_ids = $this->_get_goods_ids();
        $conditions = sprintf(' item_id IN(%s)', implode(',', $goods_ids));
        $goodsqa_list = $this->_goods_qa_mod->find(
            array(
                'conditions' => $conditions,
                'join' => 'belongs_to_user',
                'fields' => 'goods_qa.*,member.user_name',
            )
        );
        $this->echo_result($goodsqa_list);
    }

    /**
     * 获得商品咨询（带有分页信息）
     * @page integer 当前页数  默认为1
     * @limit integer 每页显示记录的条数  默认设置为10
     * @id string  required 多个id用","分割
     */
    function apiGetGoodsqaWithPager()
    {
        $goods_ids = $this->_get_goods_ids();
        $conditions = sprintf(' item_id IN(%s)', implode(',', $goods_ids));
        $per_page = isset($_REQUEST['limit']) ? $_REQUEST['limit'] : 10;
        $page_info = $this->_get_page($per_page);
        $order = isset ($_REQUEST['order']) ? $_REQUEST['order'] : 'ques_id';
        $order_type = isset ($_REQUEST['order_type']) ? $_REQUEST['order_type'] : 'DESC';
        $goodsqa_list = $this->_goods_qa_mod->find(
            array(
                'conditions' => $conditions,
                'join' => 'belongs_to_user',
                'fields' => 'goods_qa.*,member.user_name',
                'count' => true,
                'limit' => $page_info['limit'],
                'order' => "{$order} {$order_type}",
            )
        );

        $item_count = $this->_goods_qa_mod->getCount();
        $final_data = array('items' => $goodsqa_list, 'item_count' => $item_count);
        $this->echo_result($final_data);

    }
}